package com.xcode.xandrdb.Executer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import android.app.Application;
import android.database.Cursor;
import android.util.Log;

import com.xcode.xandrdb.Catch.CatchObject;
import com.xcode.xandrdb.Catch.DefaultSelectCatch;
import com.xcode.xandrdb.Executer.Core.selectExecuterCore;
import com.xcode.xandrdb.Factory.SessionFactory;
import com.xcode.xandrdb.annotation.Catch;
import com.xcode.xandrdb.interfaces.HandlerExecuter;
import com.xcode.xandrdb.sqlite.SqliteHelper;

public class SelectExecuter implements HandlerExecuter
{
	@Override
	public Object execute(InvocationHandler handler, Object proxy, Method method, Object[] args,Application session,String sql)
	{
		long key = System.currentTimeMillis();
		SqliteHelper sqLiteDatabase = null;
		Cursor cursor = null;
		Object object = null;
		CatchObject catchObject = null;
		DefaultSelectCatch selectCatch = null;
		boolean isCatch = method.isAnnotationPresent(Catch.class);//方法是否被标记为需要缓存
		boolean openCatch = SessionFactory.getConfig().openCatch;//缓存开关是否打开
		if(isCatch && openCatch) // 如果打开缓存，并且已经标记缓存
		{
			selectCatch = DefaultSelectCatch.getInstance();
			catchObject = (CatchObject) selectCatch.getCatch(sql);//首先取缓存里的数据
			if(catchObject != null)
			{
				long flashTime = catchObject.getFlashTime();
				long now = System.currentTimeMillis();
				long catchTime = catchObject.getCatchTime();
				if(now < (catchTime + flashTime))//如果现在的时间，小于上次刷新 + 缓存时间 （缓存时间没有过期）
				{
					object = catchObject.getObject();
					if (object != null)//如果取到了缓存，则直接将缓存返回，如果没有取到缓存 则去数据库查询 并缓存起来
					{
						Log.i("XAndrDB", "DataCatch:"+sql);
						return object;
					}
				}
			}else 
			{
				catchObject = new CatchObject();
				Catch catch_Anotation = method.getAnnotation(Catch.class);
				catchObject.setCatchTime(catch_Anotation.value());
				selectCatch.putCatch(sql, catchObject);
			}
		}
		try
		{
			sqLiteDatabase = SqliteHelper.getInstance();
			cursor = sqLiteDatabase.rawQuery(sql, null);
			Log.i("XAndrDB","ExecuteSql:" + sql);
			Class<?> returnType = method.getReturnType();
			//在这里需要对返回类型进行判断
			//1.基本类型
			if(returnType.isPrimitive() || returnType.getName().startsWith("java.lang"))
			{
				object = selectExecuterCore.cusorToSimpleClass(cursor, returnType);
			}
			//2.List类型
			else if(returnType.equals(List.class))
			{
				object = selectExecuterCore.cusorToList(cursor, returnType,method);
			}
			//3.数组类型
			else if (returnType.isArray()) 
			{
				object = selectExecuterCore.cusorToArray(cursor, returnType,method);
			}//4.自定义JavaBean
			else 
			{
				object = selectExecuterCore.cusorToJavaBean(cursor, returnType);
			}
			//System.out.println(object);
		} catch (Exception e)
		{
			e.printStackTrace();
		}finally
		{
			if(cursor != null)
			{
				cursor.close();
			}
			if(sqLiteDatabase != null)
			{
				sqLiteDatabase.close(key);
			}
		}
		if(isCatch && openCatch)
		{
			catchObject.setFlashTime(System.currentTimeMillis());
			catchObject.setObject(object);
		}
		
		return object;
	}
}
